package com.example.myapplication.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;
import com.example.myapplication.db.Data;
import com.example.myapplication.services.MQTTService;
import com.shy.rockerview.MyRockerView;

public class CarControlActivity extends AppCompatActivity {
    private MyRockerView mRockerViewXY;
    private TextView directionXY_Text;
    private TextView angleXY_Text;
    private TextView levelXY_Text;
    private MyRockerView mRockerViewZ;
    private TextView directionZ_Text;
    private TextView angleZ_Text;
    private TextView levelZ_Text;
    private String GatewayID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_carcontrol);
        mRockerViewXY = findViewById(R.id.rockerXY_View);
        directionXY_Text = findViewById(R.id.directionXY_Text);
        angleXY_Text = findViewById(R.id.angleXY_Text);
        levelXY_Text = findViewById(R.id.levelXY_Text);
//        mRockerViewZ = findViewById(R.id.rockerZ_View);
//        directionZ_Text = findViewById(R.id.directionZ_Text);
//        angleZ_Text = findViewById(R.id.angleZ_Text);
//        levelZ_Text = findViewById(R.id.levelZ_Text);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if(bundle != null) {
            GatewayID = bundle.getString(Data.COLUMN_Gateway);
        }


        mRockerViewXY.setOnShakeListener(MyRockerView.DirectionMode.DIRECTION_8, new MyRockerView.OnShakeListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void direction(MyRockerView.Direction direction) {
                String directionXY = "当前方向：";
                if(direction.equals(MyRockerView.Direction.DIRECTION_CENTER)){
                    angleXY_Text.setText("当前角度：");
                    levelXY_Text.setText("当前距离级别：");
                    directionXY = "当前方向：中心";
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_DOWN)){
                    directionXY = "当前方向：下";
                    Bundle bundle = new Bundle();
                    bundle.putString(Data.COLUMN_Gateway, GatewayID);
                    bundle.putString(Data.CarControlCMD, Data.CONTROLBack);
                    sendCarControlCMD(bundle);
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_LEFT)){
                    directionXY = "当前方向：左";
                    Bundle bundle = new Bundle();
                    bundle.putString(Data.COLUMN_Gateway, GatewayID);
                    bundle.putString(Data.CarControlCMD, Data.CONTROLLeftTurn);
                    sendCarControlCMD(bundle);
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_UP)){
                    directionXY = "当前方向：上";
                    Bundle bundle = new Bundle();
                    bundle.putString(Data.COLUMN_Gateway, GatewayID);
                    bundle.putString(Data.CarControlCMD, Data.CONTROLAdvance);
                    sendCarControlCMD(bundle);
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_RIGHT)){
                    directionXY = "当前方向：右";
                    Bundle bundle = new Bundle();
                    bundle.putString(Data.COLUMN_Gateway, GatewayID);
                    bundle.putString(Data.CarControlCMD, Data.CONTROLRightTurn);
                    sendCarControlCMD(bundle);
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_DOWN_LEFT)){
                    directionXY = "当前方向：左下";
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_DOWN_RIGHT)){
                    directionXY = "当前方向：右下";
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_UP_LEFT)){
                    directionXY = "当前方向：左上";
                }else if(direction.equals(MyRockerView.Direction.DIRECTION_UP_RIGHT)){
                    directionXY = "当前方向：右上";
                }
                directionXY_Text.setText(directionXY);
            }

            @Override
            public void onFinish() {

            }
        });
        mRockerViewXY.setOnAngleChangeListener(new MyRockerView.OnAngleChangeListener() {
            @Override
            public void onStart() {

            }

            @Override
            public void angle(double angle) {
                String angleXY = "当前角度：" + String.valueOf(angle);
                angleXY_Text.setText(angleXY);
            }

            @Override
            public void onFinish() {

            }
        });
        mRockerViewXY.setOnDistanceLevelListener(new MyRockerView.OnDistanceLevelListener() {
            @Override
            public void onDistanceLevel(int level) {
                String levelXY = "当前距离级别：" + String.valueOf(level);
                levelXY_Text.setText(levelXY);
            }
        });
    }

    public void sendCarControlCMD(Bundle bundle) {
        String GatewayID = bundle.getString(Data.COLUMN_Gateway);
        String ControlCMD = bundle.getString(Data.CarControlCMD);
        byte checkSum;
        byte[] cmd = new byte[6];
        cmd[0] = 0x1A;
        cmd[1] = 0x2B;
        if (ControlCMD.equals(Data.CONTROLAdvance)) {
            cmd[2] = 0x40; //01 00 00 00 前 后 左 右
        } else if (ControlCMD.equals(Data.CONTROLLeftTurn)) {
            cmd[2] = 0x04;  //00 00 01 00 前 后 左 右
        } else if (ControlCMD.equals(Data.CONTROLRightTurn)) {
            cmd[2] = 0x01; //00 00 00 01 前 后 左 右
        } else if (ControlCMD.equals(Data.CONTROLBack)) {
            cmd[2] = 0x10; //00 01 00 00 前 后 左 右
        }
        checkSum = (byte) (cmd[0] + cmd[1] + cmd[2]);
        cmd[3] = checkSum;
        cmd[4] = 0x2B;
        cmd[5] = 0x1A;

        Message message = new Message();
        Bundle bd = new Bundle();
        bd.putByteArray(Data.MQTTPublishMessage, cmd);
        message.what = 32;
        message.obj = GatewayID + Data.TopicControlCar;
        message.setData(bd);

        MQTTService.handler.sendMessage(message);//Tips:节点名_命令
//        mqtt.publishMessage(GatewayID + Data.TopicControlCar, cmd);//Tips:节点名_命令
    }
}
